#include "NotificationSystem.h"
#include "../Impp.h"

namespace Impp
{

NotificationSystem::NotificationSystem()
{
}

NotificationSystem::~NotificationSystem()
{
	std::vector<UnreadMsgNotify*>::iterator dIter(unreadMsgs.begin());
	while (dIter != unreadMsgs.end()) {
		UnreadMsgNotify* cur = *dIter;
		delete cur;
	    ++dIter;
	}
	std::vector<AuthorisationRequest*>::iterator authIter(authRequests.begin());
	while (authIter != authRequests.end()) {
		AuthorisationRequest* cur = *authIter;
		delete cur;
	    ++authIter;
	}
}

void NotificationSystem::registerUnreadMsg(MessageEvent & event){
	UnreadMsgNotify* notify = new UnreadMsgNotify(event);
	unreadMsgs.push_back(notify);
}

void NotificationSystem::unregisterAllUnreadMsgs(Buddy* buddy){
	if(unreadMsgs.empty()){
		return;
	}
	std::vector<UnreadMsgNotify*>::iterator dIter(unreadMsgs.begin());

	while (dIter != unreadMsgs.end()) {
		UnreadMsgNotify* cur = *dIter;
		if(cur->getEvent()->getBuddy() == buddy){
			dIter = unreadMsgs.erase(dIter);
			delete cur;
		}else{
		    ++dIter;
		}
	}
}

Buddy* NotificationSystem::getNextBuddyWithUnreadMsgs(){
	if(unreadMsgs.empty()){
		return NULL;
	}
	return unreadMsgs.front()->getEvent()->getBuddy();
}

void NotificationSystem::registerAuthRequest(Buddy* buddy,const wxString & reason){
	authRequests.push_back(new AuthorisationRequest(buddy));
}

}
